package com.hyy.controller;

/**
 * @author: xiongzhe
 * @Date: 2017/11/14 14:16
 */
import java.io.*;
import java.net.*;

public class MyServer {
    public static void main(String[] args) throws IOException {
        ServerSocket s = new ServerSocket(6666);
        System.out.println("开始：" + s);
        try {
            Socket socket = s.accept();
            try {
                System.out.println("连接接受" + socket);
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                while (true) {
                    char b;
                    StringBuilder buffer = new StringBuilder();
                    while ((b = (char) in.read()) != -1) {
                        if (b == 0x0) {
                            // 测试in.read();
                            break;
                        }
                        buffer.append(b);
                    }
                    String str = buffer.toString();
                    if ("q".equals(str)) {
                        break;
                    }
                    System.out.println("<<" + str);
                    out.print(str);
                    out.write(0x0);
                    out.println();
                    out.flush();
                }
            } finally {
                System.out.println("关闭....");
                socket.close();
            }
        } finally {
            s.close();
        }
    }
}
